<?php

/**
 * Created by PhpStorm.
 * User: superx
 * Date: 14-4-24
 * Time: 下午1:37
 */
class Task_Tools_Gen extends Core_Task {

    public $t;
    public $name;
    public $className;

    public function run() {
        $this->t = strtolower($this->params['t']);
        $this->name = $this->params['name'];
        if (!$this->name) {
            $this->name = ucfirst($this->t);
        }
        $this->className = str_replace('/', '_', $this->name);

        $this->genDao();
        $this->genTable();
        $this->genModel();
    }

    public function genModel() {
        $c = Core_Db_Connection::getInstance();
        $desc = $c->query("desc {$this->t}")->fetchAll(PDO::FETCH_CLASS);

        $tpl = <<<TPL
<?php

/**
 */
class Model_{$this->className} extends Core_Model {

TPL;
        foreach ($desc as $row) {
            $tpl .= "    public \${$row->Field};\n";
        }

        $tpl .= <<<TPL


    public function propMap(\$name) {
        switch (\$name) {
            default:
                break;
        }
    }

    public function rules() {

    }
}
TPL;
        $f = PROJECT_PATH . '/Model/' . $this->name . '.php';
        $this->write($f, $tpl);
    }

    public function genTable() {
        $c = Core_Db_Connection::getInstance();
        $desc = $c->query("desc {$this->t}")->fetchAll(PDO::FETCH_CLASS);
        $pk = 'id';
        foreach ($desc as $row) {
            if ($row->Key == 'PRI') {
                $pk = $row->Field;
            }
        }
        $tpl = <<<TPL
<?php

/**
 */
class Table_{$this->className} extends Core_Db_Table {

    protected \$_tablename = '{$this->t}';
    protected \$_pk = '{$pk}';

    /**
     *
     * @return Table_{$this->className}
     */
    public static function getInstance() {
        return parent::getInstance();
    }
}
TPL;
        $f = PROJECT_PATH . '/Table/' . $this->name . '.php';
        $this->write($f, $tpl);
    }

    public function genDao() {
        $f = PROJECT_PATH . '/Dao/' . $this->name . '.php';
        $tpl = <<<TPL
<?php
/**
 */
class Dao_{$this->className} extends Core_Dao {
    /**
     *
     * @return Dao_{$this->className}
     */
    public static function getInstance() {
        return parent::getInstance();
    }

}
TPL;
        $this->write($f, $tpl);
    }

    public function write($filename, $content) {
        $dir = dirname($filename);
        if (!is_dir($dir)) {
            mkdir($dir, 0777, true);
        }
        $fp = fopen($filename, 'w');
        fwrite($fp, $content);
        fclose($fp);
    }

    public function paramRules() {
        return array(
            't' => array(
                'filters' => array(
                    new Core_Filter_Trim()
                ),
                'validators' => array(
                    new Core_Validator_NotEmpty()
                )
            ),
            'name' => array(
                'filters' => array(
                    new Core_Filter_Trim()
                ),
                'default' => ''
            ),

        );
    }
}